﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Windows.Data.DomainServices;
using MVVMtutorialAdvanced.Web;

// Design time data
// http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/30/sample-data-in-the-wpf-and-silverlight-designer.aspx

// Introducing An MVVM-Friendly DomainDataSource: The DomainCollectionView
// http://blogs.msdn.com/b/kylemc/archive/2010/12/02/introducing-an-mvvm-friendly-domaindatasource-the-domaincollectionview.aspx

// DomainCollectionView Updates for Mix ‘11
// http://blogs.msdn.com/b/kylemc/archive/2011/04/15/domaincollectionview-updates-for-mix-11.aspx

namespace MVVMtutorialAdvanced.DomainServices
{
	public class DomainServiceBookMock : IDomainServiceBook
	{
		public void GetCountries(Action<IEnumerable<CountryDTO>, Exception> callback)
		{
			var data = new List<CountryDTO>();
			data.Add(new CountryDTO { Id = 1, Alias = "-" });
			data.Add(new CountryDTO { Id = 2, Alias = "Schweiz" });
			data.Add(new CountryDTO { Id = 3, Alias = "Deutschland" });

			callback(data.AsEnumerable(), null);
		}

		public void GetAuthorsQ(Action<IEnumerable<AuthorQDTO>, Exception> callback)
		{
			var data = new List<AuthorQDTO>();
			data.Add(new AuthorQDTO { Id = 1, FirstName = "Author 1", LastName = "l1" });
			data.Add(new AuthorQDTO { Id = 2, FirstName = "Author 2", LastName = "l2" });

			callback(data.AsEnumerable(), null);
		}

		#region Authors

		// Sample with DomainDataSource: DomainDataSource provides DesignData, which does not work in MVVM -> see Introducing An MVVM-Friendly DomainDataSource: The DomainCollectionView

		public void SetDomainDataSourceAuthors(DomainDataSource domainDataSource)
		{
			var data = new List<System.ServiceModel.DomainServices.Client.Entity>();

			data.Add(new Author { Id = 1, FirstName = "First 1", LastName = "Last 1" });
			data.Add(new Author { Id = 2, FirstName = "First 2", LastName = "Last 2" });

			domainDataSource.DesignData = data;  // Does not work TODO -> ask RIA-Team
		}

		public void GetAuthors(Action<Exception> callback)
		{
			callback(null);
		}

		public void InsertAuthor(Author author, Action<Exception> callback) { }
		public void DeleteAuthor(Author author, Action<Exception> callback) { }
		public void UpdateAuthor(Author author, Action<Exception> callback) { }

		#endregion Authors

		#region Books

		private Action<DomainCollectionView<BookDTO>, Exception> _getBooksCallback;
		private DomainCollectionView<BookDTO> _domainCollectionViewBooks;

		// Sample with MVVM friendly DomainCollectionView

		public void GetBooks(Action<DomainCollectionView<BookDTO>, Exception> callback)
		{
			_getBooksCallback = callback;

			// Design Time Data
			var data = new List<BookDTO>();

			var streamResourceInfo = Application.GetResourceStream(new Uri("/MVVMtutorialAdvanced;component/DomainServices/coverMock.jpg", UriKind.Relative));
			var bytes = new byte[streamResourceInfo.Stream.Length];
			streamResourceInfo.Stream.Read(bytes, 0, (int)streamResourceInfo.Stream.Length);

			var book1 = new BookDTO { Id = 1, Title = "Titel 1", Price = 1.2M, IsOutOfStock = true, Release = new DateTime(1995, 12, 23), CountryId = 3, Cover = bytes };
			var bookAuthor1 = new BookAuthorDTO { Id = 1, BookId = 1, AuthorId = 1};
			book1.BookAuthorDtos.Add(bookAuthor1);

			data.Add(book1);

			data.Add(new BookDTO { Id = 2, Title = "Title 2", Price = 2.5M, IsOutOfStock = false });

			// Create DomainCollectionView
			var loader = new DesignTimeLoader(OnGetBooksCompleted);
			_domainCollectionViewBooks = new DomainCollectionView<BookDTO>(loader, data);

			_domainCollectionViewBooks.SortDescriptions.Add(
					new SortDescription("Title", ListSortDirection.Ascending));

			_domainCollectionViewBooks.PageSize = 2;
			_domainCollectionViewBooks.MoveToFirstPage();
		}

		private void OnGetBooksCompleted(AsyncCompletedEventArgs arg)
		{
			_getBooksCallback(_domainCollectionViewBooks, null);
		}

		private class DesignTimeLoader : CollectionViewLoader
		{
			private readonly Action<AsyncCompletedEventArgs> _callbackCompleted;

			public DesignTimeLoader(Action<AsyncCompletedEventArgs> callbackCompleted)
			{
				_callbackCompleted = callbackCompleted;
			}

			public override bool CanLoad { get { return true; } }
			public override void Load(object userState)
			{
				_callbackCompleted(new AsyncCompletedEventArgs(null, false, userState));
			}
		}

		public void InsertBook(BookDTO book, Action<Exception> callback) { }
		public void DeleteBook(BookDTO book, Action<Exception> callback) { }
		public void UpdateBook(BookDTO book, Action<Exception> callback) { }

		#endregion Books


		public void RejectChanges() { }
		public void AcceptChanges() { }
	}
}
